﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Media.Effects;

namespace IndexMarkSilverlightClient.Classes
{
    public class ButtonBehavior : Behavior<UIElement>
    {
        private readonly SkewTransform _transofrmation = new SkewTransform();
        private readonly Storyboard _showStoryboard = new Storyboard();

        protected override void OnAttached()
        {
            base.OnAttached();

            if (AssociatedObject.RenderTransform == null)
            {
                AssociatedObject.RenderTransform = _transofrmation;
            }
            else if (AssociatedObject.RenderTransform is TransformGroup)
            {
                ((TransformGroup)AssociatedObject.RenderTransform).Children.Add(_transofrmation);
            }
            else
            {
                var transformGroup = new TransformGroup();
                var sourceTransform = AssociatedObject.RenderTransform;
                transformGroup.Children.Add(sourceTransform);
                transformGroup.Children.Add(_transofrmation);

                AssociatedObject.RenderTransform = transformGroup;
            }

            AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;

            var animation = new DoubleAnimationUsingKeyFrames();
            animation.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.Zero), Value = 0 });
            animation.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(50)), Value = 2.5 });
            animation.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(150)), Value = -2.5 });
            //animation.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(250)), Value = 2.5 });
            //animation.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(350)), Value = -2.5 });
            //animation.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(450)), Value = 0 });
            animation.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(250)), Value = 0 });

            var animation2 = new DoubleAnimationUsingKeyFrames();
            animation2.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.Zero), Value = 0 });
            animation2.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(50)), Value = 2.5 });
            animation2.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(150)), Value = -2.5 });
            //animation2.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(250)), Value = 2.5 });
            //animation2.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(350)), Value = -2.5 });
            //animation2.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(450)), Value = 0 });
            animation2.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(250)), Value = 0 });

            Storyboard.SetTarget(animation, _transofrmation);
            Storyboard.SetTarget(animation2, _transofrmation);
            Storyboard.SetTargetProperty(animation, new PropertyPath(SkewTransform.AngleXProperty));
            Storyboard.SetTargetProperty(animation2, new PropertyPath(SkewTransform.AngleYProperty));
            _showStoryboard.Children.Add(animation);
            _showStoryboard.Children.Add(animation2);

            AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
            AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
            base.OnDetaching();
        }


        void AssociatedObject_MouseEnter(object sender, MouseEventArgs e)
        {
            _showStoryboard.Begin();

            DropShadowEffect myDropShadowEffect = new DropShadowEffect();

            Color myShadowColor = new Color();
            myShadowColor.A = 100;
            myShadowColor.B = 100;
            myShadowColor.G = 100;
            myShadowColor.R = 100;

            myDropShadowEffect.Color = myShadowColor;
            myDropShadowEffect.Direction = 320;
            myDropShadowEffect.ShadowDepth = 5;
            myDropShadowEffect.BlurRadius = 3;
            myDropShadowEffect.Opacity = 0.5;

            AssociatedObject.Effect = myDropShadowEffect;

        }

        void AssociatedObject_MouseLeave(object sender, MouseEventArgs e)
        {
            AssociatedObject.Effect = null;
        }
    }
}
